package lhc.alg.top100;

import java.util.LinkedList;
import java.util.Queue;

/**
 * description: https://leetcode.cn/problems/implement-stack-using-queues/
 * author: hongchen.liao
 * date:  2022/8/11
 */
public class _225_Implement_Stack_using_Queues {

    class MyStack {

        Queue<Integer> q = new LinkedList<>();
        int top_elem = 0;

        public MyStack() {

        }

        public void push(int x) {
            q.offer(x);
            top_elem = x;
        }

        public int pop() {
            int size = q.size();
            while(size > 2){
                q.offer(q.poll());
                size--;
            }
            top_elem = q.peek();
            q.offer(q.poll());
            return q.poll();
        }

        public int top() {
            return top_elem;
        }

        public boolean empty() {
            return q.isEmpty();
        }
    }

}
